var express = require('express');
var router = express.Router();
var util = require('../util/util');
var dao = require('../dao/dao');

/* GET home page. */
router.get('/', function(req, res, next) {
    if(util.removeFavicon(req, res)) {
        var code = util.getRandomString(4);
        dao.saveCode(req, res, code)
        res.render('home', { validCode: code});
    }
});

//提交登录
router.post('/login', function(req, res, next) {
    if(util.removeFavicon(req, res)) {
        util.logInfo(0, '/login', 'POST', req.body);
        dao.login(req, res);
    }
});

//提交登出
router.post('/logout', function(req, res, next) {
    if(util.removeFavicon(req, res)) {
        util.logInfo(0, '/logout', 'POST', req.body);
        dao.logout(req, res);
    }
});

//错误信息
router.get('/error', function(req, res, next) {
    if(util.removeFavicon(req, res)) {
        util.logInfo(0, '/error', 'GET', req.query);
        var msg = null;
        if(util.isParameterEmpty(req.query.message)) {
            msg = {message:'请求出错了~'}
        } else {
            msg = {message:req.query.message}
        }
        res.render('CustomerError',msg);
    }
});

module.exports = router;
